// DialogReportModel.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "DialogReportModel.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogReportModel dialog


CDialogReportModel::CDialogReportModel(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogReportModel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogReportModel)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDialogReportModel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogReportModel)
	DDX_Control(pDX, IDC_STATIC_1, m_stcList);
	DDX_Control(pDX, IDC_EDIT_2, m_edtResult);
	DDX_Control(pDX, IDC_EDIT_1, m_edtDesc);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogReportModel, CDialog)
	//{{AFX_MSG_MAP(CDialogReportModel)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_MYLBUTTONDBLCLK, OnMyLButtonDblClk)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogReportModel message handlers

BOOL CDialogReportModel::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	InitCtrls();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogReportModel::InitCtrls()
{
	BOOL bRet = m_listModel.Create(WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(0,0,0,0),this,0x1001);	
	m_listModel.InitializeGridModel();

	CRect rcList;
	m_stcList.GetWindowRect(&rcList);
	ScreenToClient(&rcList);
	m_listModel.MoveWindow(rcList);

	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);
	BCDBDataSets* pDataSets = NULL;
	BCDBCreateDataSets(&pDataSets, szErrorMsg);
	BCDBDataSet* pDataSet = NULL;
	BCDBCreateDataSet(&pDataSet, szErrorMsg);
	
	bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)theApp.m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return;
	if(!BCDBQueryDBData(&DBConn, T_MODEL, pDataSet, &pDataSets, szErrorMsg)){
		BCDBDisconnectionDB(&DBConn, szErrorMsg);
		BCDBClearDataSet(pDataSet, szErrorMsg);
		BCDBClearDataSets(pDataSets, szErrorMsg);
		
		return;
	}
	BCDBDisconnectionDB(&DBConn, szErrorMsg);
	
	m_listModel.BuildModelList(pDataSets);
	
	BCDBClearDataSet(pDataSet, szErrorMsg);
	BCDBClearDataSets(pDataSets, szErrorMsg);
}
void CDialogReportModel::OnMyLButtonDblClk(WPARAM wParam, LPARAM lParam)
{
	CString strContent = "", strDescript = "";;
	strContent.Format("%s", wParam);
	strDescript.Format("%s", lParam);
	strDescript.TrimLeft();
	strDescript.TrimRight();
	strContent.TrimLeft();
	strContent.TrimRight();
	if(strContent.GetLength() > 0){
		m_edtResult.SetWindowText(strContent);
		m_edtResult.UpdateData(FALSE);
	}else{
		m_edtResult.SetWindowText("");
		m_edtResult.UpdateData(FALSE);
	}
	if (strDescript.GetLength() > 0) {
		m_edtDesc.SetWindowText(strDescript);
		m_edtDesc.UpdateData(FALSE);
	}else{
		m_edtDesc.SetWindowText("");
		m_edtDesc.UpdateData(FALSE);
	}
}

void CDialogReportModel::OnButtonAdd() 
{
	CString strContent = "", strDescript = "";
	m_edtResult.GetWindowText(strContent);
	strContent.TrimLeft();
	strContent.TrimRight();
	m_edtDesc.GetWindowText(strDescript);
	strDescript.TrimLeft();
	strDescript.TrimRight();

	if (strContent.GetLength() > 0){
		m_listModel.InsertModel(strContent, strDescript);
	}
}

void CDialogReportModel::OnButtonUpdate() 
{
	CString strContent = "", strDescript = "";
	m_edtResult.GetWindowText(strContent);
	strContent.TrimLeft();
	strContent.TrimRight();
	m_edtDesc.GetWindowText(strDescript);
	strDescript.TrimLeft();
	strDescript.TrimRight();
	if (strContent.GetLength() > 0){
		m_listModel.UpdateModel(strContent, strDescript);
	}		
}

void CDialogReportModel::OnButtonDelete() 
{
	m_listModel.DeleteSelRecord();
}
